﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.RobotJog.Controls.CUiCoordinateReference
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using System;
using System.CodeDom.Compiler;
using System.ComponentModel;
using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Markup;
using Tecnomatix.Engineering;
using Tecnomatix.Engineering.Ui.WPF.UserControls;


namespace DnProcessSimulateCommands.RobotJog.Controls;

public class CUiCoordinateReference : UserControl, IComponentConnector
{
  private ITxLocatableObject _movedObject;
  private TxTransformation _locationRelativeFrame = new TxTransformation();
  private TxTransformation _originalFrame = new TxTransformation();
  private bool _isAbsoluteLocationChangedIgnored;
  private bool _isLoadedCalled;
  internal bool _isInFreeze;
  public static readonly DependencyProperty ReferenceFrameProperty = DependencyProperty.Register(nameof (ReferenceFrame), typeof (TxTransformation), typeof (CUiCoordinateReference), (PropertyMetadata) new FrameworkPropertyMetadata((object) new TxTransformation(), new PropertyChangedCallback(CUiCoordinateReference.OnReferenceFrameChanged)));
  public static readonly DependencyProperty LocationRelativeProperty = DependencyProperty.Register(nameof (LocationRelative), typeof (Frames), typeof (CUiCoordinateReference), (PropertyMetadata) new FrameworkPropertyMetadata((object) Frames.Working, new PropertyChangedCallback(CUiCoordinateReference.OnLocationRelativeChanged)));
  public static readonly DependencyProperty TranslateStepValueProperty = DependencyProperty.Register(nameof (TranslateStepValue), typeof (double), typeof (CUiCoordinateReference), (PropertyMetadata) new FrameworkPropertyMetadata((object) 1.0, new PropertyChangedCallback(CUiCoordinateReference.OnTranslateStepValueChanged)));
  public static readonly DependencyProperty RotateStepValueProperty = DependencyProperty.Register(nameof (RotateStepValue), typeof (double), typeof (CUiCoordinateReference), (PropertyMetadata) new FrameworkPropertyMetadata((object) 1.0, new PropertyChangedCallback(CUiCoordinateReference.OnRotateStepValueChanged)));
  public static readonly DependencyProperty SnapToStepSizeProperty = DependencyProperty.Register(nameof (SnapToStepSize), typeof (bool), typeof (CUiCoordinateReference), (PropertyMetadata) new FrameworkPropertyMetadata((object) true, new PropertyChangedCallback(CUiCoordinateReference.OnSnapToStepSizeChanged)));
  internal CUiCoordinateReference CoordinateReference;
  internal TextBlock labelLocationRelative;
  internal ComboBox comboBoxFrameOfReference;
  internal TxLocationGrid locationGrid;
  internal CheckBox checkBoxSnapToStepSize;
  private bool _contentLoaded;

  public CUiCoordinateReference()
  {
    this.InitializeComponent();
    this.IsEnabled = false;
  }

  private void CoordinateReference_Loaded(object sender, RoutedEventArgs e)
  {
    if (this._isLoadedCalled)
      return;
    this.locationGrid.ObjectLocationChanged += new TxLocationGrid_ObjectLocationChangedEventHandler(this.LocationGrid_ObjectLocationChanged);
    if (!DesignerProperties.GetIsInDesignMode((DependencyObject) this))
    {
      this.SnapToStepSize = !this.SnapToStepSize;
      this.SnapToStepSize = !this.SnapToStepSize;
    }
    this._isLoadedCalled = true;
  }

  private void CoordinateReference_Unloaded(object sender, RoutedEventArgs e)
  {
    this._isLoadedCalled = false;
    if (this.MovingObject != null)
      this.MovingObject = (ITxLocatableObject) null;
    this.locationGrid.ObjectLocationChanged -= new TxLocationGrid_ObjectLocationChangedEventHandler(this.LocationGrid_ObjectLocationChanged);
  }

  public FrameType FrameType { get; set; }

  public TxTransformation RobotBase { get; set; }

  public TxTransformation OriginalFrame
  {
    get => this._originalFrame;
    set
    {
      this._originalFrame = value;
      if (this.LocationRelative != Frames.Original || this._movedObject == null || this._isInFreeze)
        return;
      this.locationGrid.ObjectLocation = this.GetRelativeLocation();
    }
  }

  public TxTransformation ReferenceFrame
  {
    get => (TxTransformation) this.GetValue(CUiCoordinateReference.ReferenceFrameProperty);
    set => this.SetValue(CUiCoordinateReference.ReferenceFrameProperty, (object) value);
  }

  public static void OnReferenceFrameChanged(
    DependencyObject d,
    DependencyPropertyChangedEventArgs e)
  {
    CUiCoordinateReference coordinateReference = (CUiCoordinateReference) d;
    if (coordinateReference.LocationRelative != Frames.Reference)
      return;
    coordinateReference.LocationRelativeFrame = coordinateReference.ReferenceFrame;
  }

  public Frames LocationRelative
  {
    get => (Frames) this.GetValue(CUiCoordinateReference.LocationRelativeProperty);
    set => this.SetValue(CUiCoordinateReference.LocationRelativeProperty, (object) value);
  }

  public static void OnLocationRelativeChanged(
    DependencyObject d,
    DependencyPropertyChangedEventArgs e)
  {
    CUiCoordinateReference coordinateReference = (CUiCoordinateReference) d;
    switch (coordinateReference.LocationRelative)
    {
      case Frames.Original:
        coordinateReference.LocationRelativeFrame = coordinateReference.OriginalFrame;
        break;
      case Frames.Reference:
        coordinateReference.LocationRelativeFrame = coordinateReference.ReferenceFrame;
        break;
      case Frames.Working:
        coordinateReference.LocationRelativeFrame = TxApplication.ActiveDocument.WorkingFrame;
        break;
    }
  }

  private TxTransformation LocationRelativeFrame
  {
    get => this._locationRelativeFrame;
    set
    {
      this._locationRelativeFrame = value;
      if (this._movedObject == null || !TxTransformation.op_Inequality(this.LocationRelativeFrame, (TxTransformation) null) || this._isInFreeze)
        return;
      this.locationGrid.ObjectLocation = this.GetRelativeLocation();
    }
  }

  public double TranslateStepValue
  {
    get => (double) this.GetValue(CUiCoordinateReference.TranslateStepValueProperty);
    set => this.SetValue(CUiCoordinateReference.TranslateStepValueProperty, (object) value);
  }

  private static void OnTranslateStepValueChanged(
    DependencyObject d,
    DependencyPropertyChangedEventArgs e)
  {
    (d as CUiCoordinateReference).SetLocationGridTranslateStepValue();
  }

  private void SetLocationGridTranslateStepValue()
  {
    this.locationGrid.TranslateStepValue = this.SnapToStepSize ? this.TranslateStepValue : this.GetDefaultTranslationStepSize();
    this.FireStepSizeChanged();
  }

  public double RotateStepValue
  {
    get => (double) this.GetValue(CUiCoordinateReference.RotateStepValueProperty);
    set => this.SetValue(CUiCoordinateReference.RotateStepValueProperty, (object) value);
  }

  private static void OnRotateStepValueChanged(
    DependencyObject d,
    DependencyPropertyChangedEventArgs e)
  {
    (d as CUiCoordinateReference).SetLocationGridRotateStepValue();
  }

  private void SetLocationGridRotateStepValue()
  {
    this.locationGrid.RotateStepValue = this.SnapToStepSize ? this.RotateStepValue : this.GetDefaultRotationStepSize();
    this.FireStepSizeChanged();
  }

  public bool SnapToStepSize
  {
    get => (bool) this.GetValue(CUiCoordinateReference.SnapToStepSizeProperty);
    set => this.SetValue(CUiCoordinateReference.SnapToStepSizeProperty, (object) value);
  }

  private static void OnSnapToStepSizeChanged(
    DependencyObject d,
    DependencyPropertyChangedEventArgs e)
  {
    CUiCoordinateReference coordinateReference = d as CUiCoordinateReference;
    coordinateReference.SetLocationGridTranslateStepValue();
    coordinateReference.SetLocationGridRotateStepValue();
    coordinateReference.FireStepSizeChanged();
  }

  public ITxLocatableObject MovingObject
  {
    get => this._movedObject;
    set
    {
      if (this._movedObject != value)
      {
        this.UnregisterWorkingFrameEvents();
        this._movedObject = value;
        if (this._movedObject != null)
        {
          this.OriginalFrame = this._movedObject.AbsoluteLocation;
          this.comboBoxFrameOfReference.SelectedIndex = 2;
          if (!this._isInFreeze)
            this.locationGrid.ObjectLocation = this.GetRelativeLocation();
          this.RegisterWorkingFrameEvents();
        }
      }
      this.IsEnabled = this._movedObject != null;
    }
  }

  public event CUiCoordinateReferenceDeltaChangedEventHanlder DeltaChanged;

  public event Action StepSizeChanged;

  public double GetDefaultRotationStepSize() => 1.0;

  public double GetDefaultTranslationStepSize() => 1.0;

  private void RegisterWorkingFrameEvents()
  {
    if (TxApplication.ActiveDocument == null)
      return;
    TxApplication.ActiveDocument.WorkingFrameChanged += new TxDocument_WorkingFrameChangedEventHandler(this.ActiveDocument_WorkingFrameChanged);
  }

  private void UnregisterWorkingFrameEvents()
  {
    if (TxApplication.ActiveDocument == null)
      return;
    TxApplication.ActiveDocument.WorkingFrameChanged -= new TxDocument_WorkingFrameChangedEventHandler(this.ActiveDocument_WorkingFrameChanged);
  }

  private void ActiveDocument_WorkingFrameChanged(
    object sender,
    TxDocument_WorkingFrameChangedEventArgs args)
  {
    if (this.LocationRelative == Frames.Working)
      this.LocationRelativeFrame = TxApplication.ActiveDocument.WorkingFrame;
    this.locationGrid.ObjectLocation = this.GetRelativeLocation();
  }

  public void MovingObjectAbsoluteLocationChanged()
  {
    if (this._isAbsoluteLocationChangedIgnored || this.MovingObject == null || this._isInFreeze)
      return;
    this.locationGrid.ObjectLocation = this.GetRelativeLocation();
  }

  private void LocationGrid_ObjectLocationChanged(
    object Sender,
    TxLocationGrid_ObjectLocationChangedEventArgs args)
  {
    if (this.DeltaChanged == null || this.MovingObject == null)
      return;
    TxTransformation delta = new TxTransformation(args.Delta.Translation, args.Delta.Rotation, (TxTransformation.TxRotationType) 1);
    this._isAbsoluteLocationChangedIgnored = true;
    int num = this.DeltaChanged((object) this, new CUiCoordinateReferenceDeltaChangedEventArgs(delta, this.LocationRelativeFrame)) ? 1 : 0;
    this._isAbsoluteLocationChangedIgnored = false;
    this.MovingObjectAbsoluteLocationChanged();
  }

  private void comboBoxFrameOfReference_SelectionChanged(object sender, SelectionChangedEventArgs e)
  {
    this.LocationRelative = (Frames) Enum.Parse(typeof (Frames), (e.AddedItems[0] as ComboBoxItem).Tag.ToString(), false);
  }

  private void FireStepSizeChanged()
  {
    if (this.StepSizeChanged == null)
      return;
    this.StepSizeChanged();
  }

  private TxTransformation GetRelativeLocation()
  {
    if (this.FrameType == FrameType.Self && this.LocationRelative == Frames.Reference)
      return new TxTransformation();
    return this.FrameType == FrameType.TCPFWithBaseOrientation && this.LocationRelative == Frames.Reference ? TxTransformation.op_Multiply(new TxTransformation(this._movedObject.AbsoluteLocation.Translation, this.RobotBase.RotationRPY_XYZ, (TxTransformation.TxRotationType) 1).Inverse, this._movedObject.AbsoluteLocation) : TxTransformation.op_Multiply(this.LocationRelativeFrame.Inverse, this._movedObject.AbsoluteLocation);
  }

  [DebuggerNonUserCode]
  [GeneratedCode("PresentationBuildTasks", "4.0.0.0")]
  public void InitializeComponent()
  {
    if (this._contentLoaded)
      return;
    this._contentLoaded = true;
    Application.LoadComponent((object) this, new Uri("/DnProcessSimulateCommands;component/robotjog/controls/cuicoordinatereference.xaml", UriKind.Relative));
  }

  [DebuggerNonUserCode]
  [GeneratedCode("PresentationBuildTasks", "4.0.0.0")]
  [EditorBrowsable(EditorBrowsableState.Never)]
  void IComponentConnector.Connect(int connectionId, object target)
  {
    switch (connectionId)
    {
      case 1:
        this.CoordinateReference = (CUiCoordinateReference) target;
        this.CoordinateReference.Loaded += new RoutedEventHandler(this.CoordinateReference_Loaded);
        this.CoordinateReference.Unloaded += new RoutedEventHandler(this.CoordinateReference_Unloaded);
        break;
      case 2:
        this.labelLocationRelative = (TextBlock) target;
        break;
      case 3:
        this.comboBoxFrameOfReference = (ComboBox) target;
        this.comboBoxFrameOfReference.SelectionChanged += new SelectionChangedEventHandler(this.comboBoxFrameOfReference_SelectionChanged);
        break;
      case 4:
        this.locationGrid = (TxLocationGrid) target;
        break;
      case 5:
        this.checkBoxSnapToStepSize = (CheckBox) target;
        break;
      default:
        this._contentLoaded = true;
        break;
    }
  }
}
